package com.viyu.alianlk.icons.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.domob.android.ads.DomobAdView;

import com.viyu.alianlk.icons.R;
import com.viyu.alianlk.icons.views.GameBoardView;

/**
 * 
 * @author Viyu_Lu
 * 
 */
public class StartBoardActivity extends Activity implements OnClickListener {

	public static final String KEY_SHAREDPREFERENCE_RECORDS = "Records";
	public static final String KEY_SHAREDPREFERENCE_RECORDS_JIANDAN = "Record_Jiandan";
	public static final String KEY_SHAREDPREFERENCE_RECORDS_ZHONGDENG = "Record_Zhongdeng";
	public static final String KEY_SHAREDPREFERENCE_RECORDS_GAOJI = "Record_Gaoji";
	public static final String KEY_SHAREDPREFERENCE_RECORDS_TIAOZHAN = "Record_Tiaozhan";

	public static final String KEY_INTENT_DIFFTYPE = "DiffType";
	public static final int DIFFTYPE_JIANDAN = 0;
	public static final int DIFFTYPE_ZHONGDENG = 1;
	public static final int DIFFTYPE_GAOJI = 2;
	public static final int DIFFTYPE_TIAOZHAN = 3;

	private LinearLayout jiandanButton = null;
	private LinearLayout zhongdengButton = null;
	private LinearLayout gaojiButton = null;
	private LinearLayout tiaozhanButton = null;

	private TextView jiandanRecord = null;
	private TextView zhongdengRecord = null;
	private TextView gaojiRecord = null;
	private TextView tiaozhanRecord = null;

	private Animation scaleAnimation = null;

	private MediaPlayer mediaPlayer = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//
		setContentView(R.layout.activity_startboard);
		//
		scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_anim);
		//
		jiandanButton = (LinearLayout) findViewById(R.id.startboard_playbutton_jiandan);
		jiandanButton.setOnClickListener(this);

		zhongdengButton = (LinearLayout) findViewById(R.id.startboard_playbutton_zhongdeng);
		zhongdengButton.setOnClickListener(this);

		gaojiButton = (LinearLayout) findViewById(R.id.startboard_playbutton_gaoji);
		gaojiButton.setOnClickListener(this);

		tiaozhanButton = (LinearLayout) findViewById(R.id.startboard_playbutton_tiaozhan);
		tiaozhanButton.setOnClickListener(this);

		jiandanRecord = (TextView) findViewById(R.id.startboard_jiandan_record);
		zhongdengRecord = (TextView) findViewById(R.id.startboard_zhongdeng_record);
		gaojiRecord = (TextView) findViewById(R.id.startboard_gaoji_record);
		tiaozhanRecord = (TextView) findViewById(R.id.startboard_tiaozhan_record);

		// ad
		RelativeLayout adContainer = (RelativeLayout)findViewById(R.id.startboard_adcontainer);
		DomobAdView adView = new DomobAdView(this, getString(R.string.ads_publishid), getString(R.string.ads_inlineid), DomobAdView.INLINE_SIZE_FLEXIBLE);
		adContainer.addView(adView);
		//
		mediaPlayer = MediaPlayer.create(this, R.raw.bg);
		mediaPlayer.setLooping(true);
	}

	@Override
	protected void onStart() {
		super.onStart();

		// 读取记录
		SharedPreferences prefers = getSharedPreferences(KEY_SHAREDPREFERENCE_RECORDS, Context.MODE_PRIVATE);
		{
			int record = prefers.getInt(KEY_SHAREDPREFERENCE_RECORDS_JIANDAN, -1);
			if (record > 0) {
				jiandanRecord.setText(getString(R.string.text_record, record));
			} else {
				jiandanRecord.setText(R.string.text_notplayed);
			}
		}
		{
			int record = prefers.getInt(KEY_SHAREDPREFERENCE_RECORDS_ZHONGDENG, -1);
			if (record > 0) {
				zhongdengRecord.setText(getString(R.string.text_record, record));
			} else {
				zhongdengRecord.setText(R.string.text_notplayed);
			}
		}
		{
			int record = prefers.getInt(KEY_SHAREDPREFERENCE_RECORDS_GAOJI, -1);
			if (record > 0) {
				gaojiRecord.setText(getString(R.string.text_record, record));
			} else {
				gaojiRecord.setText(R.string.text_notplayed);
			}
		}
		{
			int record = prefers.getInt(KEY_SHAREDPREFERENCE_RECORDS_TIAOZHAN, -1);
			if (record > 0) {
				tiaozhanRecord.setText(getString(R.string.text_record, record));
			} else {
				tiaozhanRecord.setText(R.string.text_notplayed);
			}
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		mediaPlayer.pause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mediaPlayer.release();
	}

	@Override
	protected void onResume() {
		super.onResume();
		jiandanButton.startAnimation(scaleAnimation);
		zhongdengButton.startAnimation(scaleAnimation);
		gaojiButton.startAnimation(scaleAnimation);
		tiaozhanButton.startAnimation(scaleAnimation);
		//
		mediaPlayer.start();
	}

	@Override
	public void onClick(View v) {
		int type = -1;
		switch (v.getId()) {
		case R.id.startboard_playbutton_jiandan:
			GameBoardView.X_COUNT = 8;
			GameBoardView.Y_COUNT = 10;
			type = DIFFTYPE_JIANDAN;
			break;
		case R.id.startboard_playbutton_zhongdeng:
			GameBoardView.X_COUNT = 9;
			GameBoardView.Y_COUNT = 12;
			type = DIFFTYPE_ZHONGDENG;
			break;

		case R.id.startboard_playbutton_gaoji:
			GameBoardView.X_COUNT = 11;
			GameBoardView.Y_COUNT = 15;
			type = DIFFTYPE_GAOJI;
			break;

		case R.id.startboard_playbutton_tiaozhan:
			GameBoardView.X_COUNT = 12;
			GameBoardView.Y_COUNT = 16;
			type = DIFFTYPE_TIAOZHAN;
			break;
		}
		Intent intent = new Intent(this, PlayBoardActivity.class);
		intent.putExtra(KEY_INTENT_DIFFTYPE, type);
		startActivity(intent);
		overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
	}
}